package com.ruoyi.APPMS.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.APPMS.domain.Buypart;
import com.ruoyi.APPMS.service.IBuypartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 购买零部件Controller
 *
 * @author 任容
 * @date 2023-07-03
 */
@Api(tags = "采购零部件")
@RestController
@RequestMapping("/buypart/buypart")
public class BuypartController extends BaseController
{
    @Autowired
    private IBuypartService buypartService;

    /**
     * 分页查询购买零部件列表
     */
    @ApiOperation("分页查询购买零部件列表")
    @PreAuthorize("@ss.hasPermi('buypart:buypart:list')")
    @GetMapping("/list")
    public TableDataInfo list(Buypart buypart)
    {
        startPage();

//        QueryWrapper<Buypart> queryWrapper=new QueryWrapper<>();

        List<Buypart> list = buypartService.list(buypart);
        return getDataTable(list);
    }

    /**
     * 导出购买零部件列表
     */
    @ApiOperation("导出购买零部件列表")
    @PreAuthorize("@ss.hasPermi('buypart:buypart:export')")
    @Log(title = "购买零部件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Buypart buypart)
    {
//        QueryWrapper<Buypart> queryWrapper=new QueryWrapper<>();

        List<Buypart> list = buypartService.list(buypart);
        ExcelUtil<Buypart> util = new ExcelUtil<Buypart>(Buypart.class);
        util.exportExcel(response, list, "购买零部件数据");
    }

    /**
     * 获取购买零部件详细信息
     */
    @ApiOperation("获取购买零部件详细信息")
    @PreAuthorize("@ss.hasPermi('buypart:buypart:query')")
    @GetMapping(value = "/{partid}")
    public AjaxResult getInfo(@PathVariable("partid") Long partid)
    {
        return success(buypartService.listByid(partid));
    }

    /**
     * 新增购买零部件
     */
    //@ApiOperation("新增购买零部件")
    @PreAuthorize("@ss.hasPermi('buypart:buypart:add')")
    @Log(title = "购买零部件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Buypart buypart)
    {
        buypart.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(buypartService.save(buypart));
    }

    /**
     * 购买零部件-下订单
     */
    /*@PreAuthorize("@ss.hasPermi('buypart:buypart:createbuyorder')")
    @Log(title = "(下订单)购买零部件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult createbuyorder(@RequestBody Buypart buypart)
    {
//        buypart.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(buypartService.save(buypart));
    }*/

    /**
     * 购买零部件
     */
    @ApiOperation("购买零部件")
    @PreAuthorize("@ss.hasPermi('buypart:buypart:edit')")
    @Log(title = "购买零部件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Buypart buypart)
    {
        buypartService.createOrder1(buypart);
        return toAjax(buypartService.createOrder2(buypart));
    }

    /**
     * 删除购买零部件
     */
    //@ApiOperation("删除购买零部件")
    @PreAuthorize("@ss.hasPermi('buypart:buypart:remove')")
    @Log(title = "购买零部件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{partids}")
    public AjaxResult remove(@PathVariable List<Long> partids)
    {
        return toAjax(buypartService.removeByIds(partids));
    }
}
